﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PropertyGridEx
{
    public partial class MultiItemEditor : Form
    {
        string _itemString = "";
        List<string> _items = null;
        char _delimiter = ';';
        MultiItemType _itemType;

        public enum MultiItemType
        {
            Path, File
        };

        public string ItemString
        {
            get
            {
                return _itemString;
            }
        }

        public MultiItemEditor(string ItemString, MultiItemType itemType, char delimiter)
        {
            InitializeComponent();
            _itemString = ItemString;
            _delimiter = delimiter;

            if (ItemString.Trim() != "")
                _items = ItemString.Split(delimiter).ToList();
            else
                _items = null;
            _itemType = itemType;

        }

        private void MultiItemEditor_Load(object sender, EventArgs e)
        {
            lvPaths.Items.Clear();
            if (_items != null)
            {
                foreach (string item in _items)
                {
                    lvPaths.Items.Add(item);
                }
            }
            lvPaths.Refresh();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string value = "";

            if (_itemType == MultiItemType.Path)
            {
                FolderBrowserDialog folderDlg = new FolderBrowserDialog();

                folderDlg.RootFolder = Environment.SpecialFolder.MyComputer;
                if (folderDlg.ShowDialog() == DialogResult.OK)
                {
                    value = folderDlg.SelectedPath;
                }
                folderDlg.Dispose();
            }
            else if (_itemType == MultiItemType.File)
            {
                FileDialog fileDlg = new SaveFileDialog();
                if (fileDlg.ShowDialog() == DialogResult.OK)
                {
                    value = fileDlg.FileName;
                }
                fileDlg.Dispose();
            }



            if (value != "")
                lvPaths.Items.Add(value);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lvPaths.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in lvPaths.Items)
                {
                    if (item.Selected)
                        lvPaths.Items.Remove(item);
                }
            }
        }

        private void MultiItemEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (ListViewItem item in lvPaths.Items)
            {
                if (sb.ToString() != "")
                    sb.Append(_delimiter);
                sb.Append(item.Text);
            }
            _itemString = sb.ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
